<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<title>Coverage for c:\program files\python26\lib\site-packages\modbus_tk-0.3-py2.6.egg\modbus_tk\modbus_rtu</title>
<link rel='stylesheet' href='style.css' type='text/css'>
<script type='text/javascript' src='jquery-1.3.2.min.js'></script>
<script type='text/javascript'>
function toggle_lines(btn, cls) {
    var btn = $(btn);
    var hide = "hide_"+cls;
    if (btn.hasClass(hide)) {
        $("#source ."+cls).removeClass(hide);
        btn.removeClass(hide);
    }
    else {
        $("#source ."+cls).addClass(hide);
        btn.addClass(hide);
    }
}
</script>
</head>
<body>
<div id='header'>
    <div class='content'>
        <h1>Coverage for <b>c:\program files\python26\lib\site-packages\modbus_tk-0.3-py2.6.egg\modbus_tk\modbus_rtu</b> :
            <span class='pc_cov'>17%</span>
        </h1>
        <h2 class='stats'>
            130 statements
            <span class='run hide_run' onclick='toggle_lines(this, "run")'>29 run</span>
            <span class='exc' onclick='toggle_lines(this, "exc")'>0 excluded</span>
            <span class='mis' onclick='toggle_lines(this, "mis")'>101 missing</span>
            
                <span class='par run hide_run' onclick='toggle_lines(this, "par")'>0 partial</span>
            
        </h2>
    </div>
</div>

<div id='source'>
<table cellspacing='0' cellpadding='0'>
<tr>
<td class='linenos' valign='top'>
<p class='pln'>1</p>
<p class='pln'>2</p>
<p class='stm run hide_run'>3</p>
<p class='pln'>4</p>
<p class='pln'>5</p>
<p class='pln'>6</p>
<p class='pln'>7</p>
<p class='pln'>8</p>
<p class='pln'>9</p>
<p class='pln'>10</p>
<p class='pln'>11</p>
<p class='pln'>12</p>
<p class='stm run hide_run'>13</p>
<p class='stm run hide_run'>14</p>
<p class='stm run hide_run'>15</p>
<p class='stm run hide_run'>16</p>
<p class='stm run hide_run'>17</p>
<p class='pln'>18</p>
<p class='pln'>19</p>
<p class='stm run hide_run'>20</p>
<p class='pln'>21</p>
<p class='pln'>22</p>
<p class='stm run hide_run'>23</p>
<p class='pln'>24</p>
<p class='stm mis'>25</p>
<p class='stm mis'>26</p>
<p class='stm mis'>27</p>
<p class='pln'>28</p>
<p class='stm run hide_run'>29</p>
<p class='pln'>30</p>
<p class='stm mis'>31</p>
<p class='stm mis'>32</p>
<p class='stm mis'>33</p>
<p class='stm mis'>34</p>
<p class='stm mis'>35</p>
<p class='stm mis'>36</p>
<p class='pln'>37</p>
<p class='stm run hide_run'>38</p>
<p class='pln'>39</p>
<p class='stm mis'>40</p>
<p class='stm mis'>41</p>
<p class='pln'>42</p>
<p class='stm mis'>43</p>
<p class='stm mis'>44</p>
<p class='stm mis'>45</p>
<p class='pln'>46</p>
<p class='pln'>47</p>
<p class='stm mis'>48</p>
<p class='pln'>49</p>
<p class='stm mis'>50</p>
<p class='stm mis'>51</p>
<p class='pln'>52</p>
<p class='stm mis'>53</p>
<p class='pln'>54</p>
<p class='stm run hide_run'>55</p>
<p class='pln'>56</p>
<p class='stm mis'>57</p>
<p class='stm mis'>58</p>
<p class='pln'>59</p>
<p class='stm mis'>60</p>
<p class='pln'>61</p>
<p class='stm mis'>62</p>
<p class='stm mis'>63</p>
<p class='stm mis'>64</p>
<p class='pln'>65</p>
<p class='stm mis'>66</p>
<p class='pln'>67</p>
<p class='stm run hide_run'>68</p>
<p class='pln'>69</p>
<p class='stm mis'>70</p>
<p class='stm mis'>71</p>
<p class='stm mis'>72</p>
<p class='stm mis'>73</p>
<p class='pln'>74</p>
<p class='pln'>75</p>
<p class='stm run hide_run'>76</p>
<p class='pln'>77</p>
<p class='pln'>78</p>
<p class='stm run hide_run'>79</p>
<p class='pln'>80</p>
<p class='stm mis'>81</p>
<p class='stm mis'>82</p>
<p class='stm mis'>83</p>
<p class='stm mis'>84</p>
<p class='stm mis'>85</p>
<p class='stm mis'>86</p>
<p class='pln'>87</p>
<p class='stm run hide_run'>88</p>
<p class='pln'>89</p>
<p class='stm mis'>90</p>
<p class='stm mis'>91</p>
<p class='stm mis'>92</p>
<p class='pln'>93</p>
<p class='stm run hide_run'>94</p>
<p class='pln'>95</p>
<p class='stm mis'>96</p>
<p class='stm mis'>97</p>
<p class='stm mis'>98</p>
<p class='pln'>99</p>
<p class='stm run hide_run'>100</p>
<p class='pln'>101</p>
<p class='stm mis'>102</p>
<p class='stm mis'>103</p>
<p class='pln'>104</p>
<p class='stm run hide_run'>105</p>
<p class='pln'>106</p>
<p class='stm mis'>107</p>
<p class='stm mis'>108</p>
<p class='stm mis'>109</p>
<p class='pln'>110</p>
<p class='stm mis'>111</p>
<p class='stm mis'>112</p>
<p class='pln'>113</p>
<p class='stm mis'>114</p>
<p class='stm mis'>115</p>
<p class='pln'>116</p>
<p class='stm run hide_run'>117</p>
<p class='pln'>118</p>
<p class='stm mis'>119</p>
<p class='stm mis'>120</p>
<p class='stm mis'>121</p>
<p class='stm mis'>122</p>
<p class='stm mis'>123</p>
<p class='stm mis'>124</p>
<p class='pln'>125</p>
<p class='pln'>126</p>
<p class='stm mis'>127</p>
<p class='pln'>128</p>
<p class='stm mis'>129</p>
<p class='stm mis'>130</p>
<p class='stm mis'>131</p>
<p class='stm mis'>132</p>
<p class='pln'>133</p>
<p class='stm run hide_run'>134</p>
<p class='pln'>135</p>
<p class='stm mis'>136</p>
<p class='pln'>137</p>
<p class='pln'>138</p>
<p class='stm run hide_run'>139</p>
<p class='pln'>140</p>
<p class='pln'>141</p>
<p class='stm run hide_run'>142</p>
<p class='pln'>143</p>
<p class='stm mis'>144</p>
<p class='stm mis'>145</p>
<p class='stm mis'>146</p>
<p class='stm mis'>147</p>
<p class='stm mis'>148</p>
<p class='stm mis'>149</p>
<p class='pln'>150</p>
<p class='stm run hide_run'>151</p>
<p class='pln'>152</p>
<p class='stm mis'>153</p>
<p class='stm mis'>154</p>
<p class='stm mis'>155</p>
<p class='stm mis'>156</p>
<p class='pln'>157</p>
<p class='stm run hide_run'>158</p>
<p class='pln'>159</p>
<p class='stm mis'>160</p>
<p class='pln'>161</p>
<p class='stm run hide_run'>162</p>
<p class='pln'>163</p>
<p class='stm mis'>164</p>
<p class='pln'>165</p>
<p class='stm run hide_run'>166</p>
<p class='pln'>167</p>
<p class='stm mis'>168</p>
<p class='stm mis'>169</p>
<p class='pln'>170</p>
<p class='stm run hide_run'>171</p>
<p class='pln'>172</p>
<p class='stm mis'>173</p>
<p class='stm mis'>174</p>
<p class='stm mis'>175</p>
<p class='pln'>176</p>
<p class='stm run hide_run'>177</p>
<p class='pln'>178</p>
<p class='stm mis'>179</p>
<p class='pln'>180</p>
<p class='stm run hide_run'>181</p>
<p class='pln'>182</p>
<p class='stm mis'>183</p>
<p class='pln'>184</p>
<p class='stm mis'>185</p>
<p class='stm mis'>186</p>
<p class='stm mis'>187</p>
<p class='stm mis'>188</p>
<p class='stm mis'>189</p>
<p class='stm mis'>190</p>
<p class='pln'>191</p>
<p class='pln'>192</p>
<p class='stm mis'>193</p>
<p class='stm mis'>194</p>
<p class='stm mis'>195</p>
<p class='stm mis'>196</p>
<p class='stm mis'>197</p>
<p class='pln'>198</p>
<p class='pln'>199</p>
<p class='stm mis'>200</p>
<p class='stm mis'>201</p>
<p class='stm mis'>202</p>
<p class='pln'>203</p>
<p class='stm mis'>204</p>
<p class='stm mis'>205</p>
<p class='stm mis'>206</p>
<p class='pln'>207</p>
<p class='stm mis'>208</p>
<p class='stm mis'>209</p>
    
</td>
<td class='text' valign='top'>
<p class='pln'><span class='com'>#!/usr/bin/env python</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='com'># -*- coding: utf-8 -*-</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'> Modbus TestKit: Implementation of Modbus protocol in python</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'></span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'> (C)2009 - Luc Jean - luc.jean@gmail.com</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'> (C)2009 - Apidev - http://www.apidev.fr</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'></span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'> This is distributed under GNU LGPL license, see license.txt</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'></span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>from</span> <span class='nam'>modbus</span> <span class='key'>import</span> <span class='op'>*</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>import</span> <span class='nam'>logging</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>import</span> <span class='nam'>sys</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>import</span> <span class='nam'>time</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>from</span> <span class='nam'>hooks</span> <span class='key'>import</span> <span class='nam'>call_hooks</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='com'>#-------------------------------------------------------------------------------</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>RtuQuery</span><span class='op'>(</span><span class='nam'>Query</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Subclass of a Query. Adds the Modbus RTU specific part of the protocol&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Constructor&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>Query</span><span class='op'>.</span><span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_request_address</span> <span class='op'>=</span> <span class='num'>0</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_response_address</span> <span class='op'>=</span> <span class='num'>0</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>build_request</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>pdu</span><span class='op'>,</span> <span class='nam'>slave</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Add the Modbus RTU part to the request&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_request_address</span> <span class='op'>=</span> <span class='nam'>slave</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>_request_address</span> <span class='op'>&lt;</span> <span class='num'>0</span><span class='op'>)</span> <span class='key'>or</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>_request_address</span> <span class='op'>&gt;</span> <span class='num'>247</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>InvalidArgumentError</span><span class='op'>,</span> <span class='str'>&quot;Invalid address %d&quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>_request_address</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>data</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;B&quot;</span><span class='op'>,</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_request_address</span><span class='op'>)</span> <span class='op'>+</span> <span class='nam'>pdu</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>crc</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;H&quot;</span><span class='op'>,</span> <span class='nam'>utils</span><span class='op'>.</span><span class='nam'>calculate_crc</span><span class='op'>(</span><span class='nam'>data</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='op'>(</span><span class='nam'>data</span> <span class='op'>+</span> <span class='nam'>crc</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>parse_response</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>response</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Extract the pdu from the Modbus RTU response&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>response</span><span class='op'>)</span> <span class='op'>&lt;</span> <span class='num'>3</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>ModbusInvalidResponseError</span><span class='op'>,</span> <span class='str'>&quot;Response length is invalid %d&quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>len</span><span class='op'>(</span><span class='nam'>response</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>_response_address</span><span class='op'>,</span> <span class='op'>)</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>unpack</span><span class='op'>(</span><span class='str'>&quot;&gt;B&quot;</span><span class='op'>,</span> <span class='nam'>response</span><span class='op'>[</span><span class='num'>0</span><span class='op'>]</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_request_address</span> <span class='op'>!=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_response_address</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>ModbusInvalidResponseError</span><span class='op'>,</span> <span class='str'>&quot;Response address %d is different from request address %d&quot;</span> <span class='op'>%</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>_response_address</span><span class='op'>,</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_request_address</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='op'>(</span><span class='nam'>crc</span><span class='op'>,</span> <span class='op'>)</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>unpack</span><span class='op'>(</span><span class='str'>&quot;&gt;H&quot;</span><span class='op'>,</span> <span class='nam'>response</span><span class='op'>[</span><span class='op'>-</span><span class='num'>2</span><span class='op'>:</span><span class='op'>]</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>crc</span> <span class='op'>!=</span> <span class='nam'>utils</span><span class='op'>.</span><span class='nam'>calculate_crc</span><span class='op'>(</span><span class='nam'>response</span><span class='op'>[</span><span class='op'>:</span><span class='op'>-</span><span class='num'>2</span><span class='op'>]</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>ModbusInvalidResponseError</span><span class='op'>,</span> <span class='str'>&quot;Invalid CRC in response&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>response</span><span class='op'>[</span><span class='num'>1</span><span class='op'>:</span><span class='op'>-</span><span class='num'>2</span><span class='op'>]</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>parse_request</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Extract the pdu from the Modbus RTU request&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>request</span><span class='op'>)</span> <span class='op'>&lt;</span> <span class='num'>3</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>ModbusInvalidRequestError</span><span class='op'>,</span> <span class='str'>&quot;Request length is invalid %d&quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>len</span><span class='op'>(</span><span class='nam'>request</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>_request_address</span><span class='op'>,</span> <span class='op'>)</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>unpack</span><span class='op'>(</span><span class='str'>&quot;&gt;B&quot;</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>[</span><span class='num'>0</span><span class='op'>]</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='op'>(</span><span class='nam'>crc</span><span class='op'>,</span> <span class='op'>)</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>unpack</span><span class='op'>(</span><span class='str'>&quot;&gt;H&quot;</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>[</span><span class='op'>-</span><span class='num'>2</span><span class='op'>:</span><span class='op'>]</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>crc</span> <span class='op'>!=</span> <span class='nam'>utils</span><span class='op'>.</span><span class='nam'>calculate_crc</span><span class='op'>(</span><span class='nam'>request</span><span class='op'>[</span><span class='op'>:</span><span class='op'>-</span><span class='num'>2</span><span class='op'>]</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>ModbusInvalidRequestError</span><span class='op'>,</span> <span class='str'>&quot;Invalid CRC in request&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>_request_address</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>[</span><span class='num'>1</span><span class='op'>:</span><span class='op'>-</span><span class='num'>2</span><span class='op'>]</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>build_response</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>response_pdu</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Build the response&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_response_address</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_request_address</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>data</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;B&quot;</span><span class='op'>,</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_response_address</span><span class='op'>)</span> <span class='op'>+</span> <span class='nam'>response_pdu</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>crc</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;H&quot;</span><span class='op'>,</span> <span class='nam'>utils</span><span class='op'>.</span><span class='nam'>calculate_crc</span><span class='op'>(</span><span class='nam'>data</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='op'>(</span><span class='nam'>data</span> <span class='op'>+</span> <span class='nam'>crc</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='com'>#-------------------------------------------------------------------------------</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='key'>class</span> <span class='nam'>RtuMaster</span><span class='op'>(</span><span class='nam'>Master</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Subclass of Master. Implements the Modbus RTU MAC layer&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>serial</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Constructor. Pass the pyserial.Serial object&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span> <span class='op'>=</span> <span class='nam'>serial</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>LOGGER</span><span class='op'>.</span><span class='nam'>info</span><span class='op'>(</span><span class='str'>&quot;RtuMaster %s is %s&quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>portstr</span><span class='op'>,</span> <span class='str'>&quot;opened&quot;</span> <span class='key'>if</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>isOpen</span><span class='op'>(</span><span class='op'>)</span> <span class='key'>else</span> <span class='str'>&quot;closed&quot;</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>Master</span><span class='op'>.</span><span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>timeout</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_t0</span> <span class='op'>=</span> <span class='nam'>utils</span><span class='op'>.</span><span class='nam'>calculate_rtu_inter_char</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>baudrate</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>interCharTimeout</span> <span class='op'>=</span> <span class='num'>1.5</span> <span class='op'>*</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_t0</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>timeout</span> <span class='op'>=</span> <span class='num'>1.5</span> <span class='op'>*</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_t0</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_do_open</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Open the given serial port if not already opened&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='key'>not</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>isOpen</span><span class='op'>(</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus_rtu.RtuMaster.before_open&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>open</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_do_close</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Close the serial port if still opened&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>isOpen</span><span class='op'>(</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>close</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus_rtu.RtuMaster.after_close&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>set_timeout</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>timeout_in_sec</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Change the timeout value&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>Master</span><span class='op'>.</span><span class='nam'>set_timeout</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>timeout_in_sec</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>timeout</span> <span class='op'>=</span> <span class='nam'>timeout_in_sec</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_send</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Send request to the slave&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>retval</span> <span class='op'>=</span> <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus_rtu.RtuMaster.before_send&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>retval</span> <span class='op'>&lt;&gt;</span> <span class='nam'>None</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>retval</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>flushInput</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>flushOutput</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>write</span><span class='op'>(</span><span class='nam'>request</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>time</span><span class='op'>.</span><span class='nam'>sleep</span><span class='op'>(</span><span class='num'>3.5</span> <span class='op'>*</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_t0</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_recv</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>expected_length</span><span class='op'>=</span><span class='op'>-</span><span class='num'>1</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Receive the response from the slave&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>=</span> <span class='str'>&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>read_bytes</span> <span class='op'>=</span> <span class='str'>&quot;dummy&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>while</span> <span class='nam'>read_bytes</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>read_bytes</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>read</span><span class='op'>(</span><span class='num'>1</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>+=</span> <span class='nam'>read_bytes</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>expected_length</span><span class='op'>&gt;=</span><span class='num'>0</span> <span class='key'>and</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>response</span><span class='op'>)</span><span class='op'>&gt;=</span><span class='nam'>expected_length</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#if the expected number of byte is received consider that the response is done</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#improve performance by avoiding end-of-response detection by timeout</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>break</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>retval</span> <span class='op'>=</span> <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus_rtu.RtuMaster.after_recv&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>response</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>retval</span> <span class='op'>&lt;&gt;</span> <span class='nam'>None</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>retval</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>response</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_make_query</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Returns an instance of a Query subclass implementing the modbus RTU protocol&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>RtuQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='com'>#-------------------------------------------------------------------------------</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='key'>class</span> <span class='nam'>RtuServer</span><span class='op'>(</span><span class='nam'>Server</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;This class implements a simple and mono-threaded modbus rtu server&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>serial</span><span class='op'>,</span> <span class='nam'>databank</span><span class='op'>=</span><span class='nam'>None</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Constructor: initializes the server settings&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>Server</span><span class='op'>.</span><span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>databank</span> <span class='key'>if</span> <span class='nam'>databank</span> <span class='key'>else</span> <span class='nam'>Databank</span><span class='op'>(</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span> <span class='op'>=</span> <span class='nam'>serial</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>LOGGER</span><span class='op'>.</span><span class='nam'>info</span><span class='op'>(</span><span class='str'>&quot;RtuServer %s is %s&quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>portstr</span><span class='op'>,</span> <span class='str'>&quot;opened&quot;</span> <span class='key'>if</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>isOpen</span><span class='op'>(</span><span class='op'>)</span> <span class='key'>else</span> <span class='str'>&quot;closed&quot;</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_t0</span> <span class='op'>=</span> <span class='nam'>utils</span><span class='op'>.</span><span class='nam'>calculate_rtu_inter_char</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>baudrate</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>interCharTimeout</span> <span class='op'>=</span> <span class='num'>1.5</span> <span class='op'>*</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_t0</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>timeout</span> <span class='op'>=</span> <span class='num'>1.5</span> <span class='op'>*</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_t0</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>close</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;close the serial communication&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>isOpen</span><span class='op'>(</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus_rtu.RtuServer.before_close&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>close</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus_rtu.RtuServer.after_close&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>__del__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Destructor&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>close</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_make_query</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Returns an instance of a Query subclass implementing the modbus RTU protocol&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>RtuQuery</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>stop</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Force the server thread to exit&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>Server</span><span class='op'>.</span><span class='nam'>stop</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>close</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_do_init</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;initialize the serial connection&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus_rtu.RtuServer.before_open&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>open</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus_rtu.RtuServer.after_open&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_do_exit</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;close the serial connection&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>close</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_do_run</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;main function of the server&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>try</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#check the status of every socket</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>=</span> <span class='str'>&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='str'>&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>read_bytes</span> <span class='op'>=</span> <span class='str'>&quot;dummy&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>while</span> <span class='nam'>read_bytes</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>read_bytes</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>read</span><span class='op'>(</span><span class='num'>128</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>+=</span> <span class='nam'>read_bytes</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#parse the request</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>request</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>retval</span> <span class='op'>=</span> <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus_rtu.RtuServer.after_read&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>retval</span> <span class='op'>&lt;&gt;</span> <span class='nam'>None</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>retval</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_handle</span><span class='op'>(</span><span class='nam'>request</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#send back the response</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>retval</span> <span class='op'>=</span> <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus_rtu.RtuServer.before_write&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>response</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>retval</span> <span class='op'>&lt;&gt;</span> <span class='nam'>None</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>=</span> <span class='nam'>retval</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>response</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_serial</span><span class='op'>.</span><span class='nam'>write</span><span class='op'>(</span><span class='nam'>response</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>time</span><span class='op'>.</span><span class='nam'>sleep</span><span class='op'>(</span><span class='num'>3.5</span> <span class='op'>*</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_t0</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>except</span> <span class='nam'>Exception</span><span class='op'>,</span> <span class='nam'>excpt</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>LOGGER</span><span class='op'>.</span><span class='nam'>error</span><span class='op'>(</span><span class='str'>&quot;Error while handling request, Exception occurred: %s&quot;</span><span class='op'>,</span> <span class='nam'>excpt</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus_rtu.RtuServer.on_error&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>excpt</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
    
</td>
</tr>
</table>
</div>

</body>
</html>
